package com.fare.taluo.core.domain.analyse.report.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.fare.taluo.core.domain.analyse.report.ZhairReportEntity;
import com.fare.taluo.core.domain.analyse.report.ZhairReportEntityRepository;
import com.fare.taluo.fetl.domain.base.EntityFetlHibernateRepository;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.StringAndDateAndFileUtil;
@Repository
public class ZhairReportEntityRepositoryImpl extends EntityFetlHibernateRepository implements ZhairReportEntityRepository{

	@Override
	public List<ZhairReportEntity> findZhairReportEntityByCondition(
			String hbaseKey, String fromCityCode, String toCityCode,
			 String flightDate) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer("from ZhairReportEntity fm where 1=1");
		
		if(StringAndDateAndFileUtil.checkString(hbaseKey)){
			sbf.append(" and fm.hbaseKey = ?");
			params.add(hbaseKey);
		}
		if(StringAndDateAndFileUtil.checkString(fromCityCode)){
			sbf.append(" and fm.fromCity = ?");
			params.add(fromCityCode);
		}
		if(StringAndDateAndFileUtil.checkString(toCityCode)){
			sbf.append(" and fm.toCity = ?");
			params.add(toCityCode);
		}
		if(StringAndDateAndFileUtil.checkString(flightDate)){
			try {
				/*Date flight = DateUtil.parse(flightDate, "yyyy-mm-dd");
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(flight);
				calendar.add(Calendar.DATE, 1);
				
				
				sbf.append(" and fm.flightDate >= ? and fm.flightDate <= ?");
				params.add(DateUtils.strToDate(flightDate,"yyyy-mm-dd"));
				params.add(calendar.getTime());*/
				sbf.append(" and fm.flightDate = ? ");
				Date date = DateUtils.strToDate(flightDate,"yyyy-mm-dd");
				date.setMinutes(0);
				params.add(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/*if(createTime != null && endCreateTime !=null){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(endCreateTime);
			calendar.add(Calendar.DATE, 1);
			//calendar.set(Calendar., value)
			hql+="and f.createTime >= ? and f.createTime <= ? ";
			list.add(createTime);
			list.add(calendar.getTime());
		}*/
		sbf.append(" order by fm.flightNo desc");
		
		Session session =getSessionFactory1().openSession();
		List<ZhairReportEntity> list = null;
		try {
			Query query = session.createQuery(sbf.toString());
			for (int i = 0; i < params.size(); i++) {
			query = query.setParameter(i, params.get(i));
			}
			 list = query.list();
		} catch (Exception e) {
			e.printStackTrace();	
		}finally{
			session.close();
		}
		
		return list;
	}
	


}
